/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Chitietdaugia;
import pojo.Daugiathanhcong;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.HibernateUtil;

/**
 *
 * @author WMThiet
 */
public class DauGiaThanhCongDAO {
    
    public static int pageCount; // Biến tính số trang, sau mỗi lần tìm kiếm sẽ cập nhật
    public static int listCount; // Biến đếm số sản phẩm
    
    public static int tinhTrangTheoDSSP(int soLuong, int soLuongSanPhamTrenTrang) {
        int soTrang = 0;

        if (soLuongSanPhamTrenTrang == -1) {
            soTrang = 1;
        } else {
            soTrang = soLuong / soLuongSanPhamTrenTrang;
            if (soLuong % soLuongSanPhamTrenTrang != 0) {
                soTrang++;
            }
        }
        return soTrang;
    }
    
    public static Daugiathanhcong LaySPDauGiaThanhCongGanNhat() {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Daugiathanhcong daugiaTC = null;
        try{            
            session.beginTransaction();            
            String hql = "from Daugiathanhcong";
            Query query = session.createQuery(hql);
            List<Daugiathanhcong> listDGTC = query.list();
            daugiaTC = listDGTC.get(listDGTC.size() - 1);
        }
        catch(Exception ex){
            daugiaTC = null;
        }finally{
            session.close();
        }
        
        return daugiaTC;
    }
    
    public static boolean  ThemDauGiaThanhCong(Chitietdaugia ctdg){
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean bDone = false;
        try{            
            session.beginTransaction();            
            
            String hql = "from Daugiathanhcong dgtc where dgtc.chitietdaugia.maChiTietdaugia =:mactdg";
            Query query = session.createQuery(hql);
            query.setInteger("mactdg", ctdg.getMaChiTietdaugia());
            
            List<Daugiathanhcong> listDGTC = query.list();
            if(listDGTC != null && listDGTC.isEmpty() == false){
                return false;
            }
            //Lưu đấu giá thành công:
            Daugiathanhcong dgtc = new Daugiathanhcong();
            dgtc.setTaikhoan(ctdg.getTaikhoan());
            dgtc.setDaNhanHang(false);
            dgtc.setGiaKhopLenh(ctdg.getGiaDau());
            dgtc.setChitietdaugia(ctdg);            
            session.save(dgtc);
            
            //Set trạng thái chiến thắng cho phiên đấu giá:
            Chitietdaugia ctCT = (Chitietdaugia)session.get(Chitietdaugia.class, ctdg.getMaChiTietdaugia());            
            ctCT.setLaLuotChienThang(true);
            session.update(ctCT);
            
            //Set trạng thái sản phẩm:
            Sanpham sp = (Sanpham)session.get(Sanpham.class, ctdg.getSanpham().getMaSanPham());
            sp.setDaXetCT(true);
            session.update(sp);
            
            //Cộng điểm đánh giá:
            Taikhoan taiKhoanNguoiDang = (Taikhoan) session.get(Taikhoan.class, sp.getTaikhoan().getMaTaiKhoan());
            taiKhoanNguoiDang.setDiemTinCayBan(taiKhoanNguoiDang.getDiemTinCayBan() + 3);
            session.update(taiKhoanNguoiDang);
            
            Taikhoan taiKhoanNguoiMua = (Taikhoan) session.get(Taikhoan.class, ctdg.getTaikhoan().getMaTaiKhoan());
            taiKhoanNguoiMua.setDiemTinCayMua(taiKhoanNguoiDang.getDiemTinCayMua() + 5);
            session.update(taiKhoanNguoiMua);
            
            session.getTransaction().commit();
            bDone = true;
        }
        catch(Exception ex){
            session.getTransaction().rollback();
            bDone = false;
        }finally{
            session.close();
        }
        
        return bDone;
    }        
    
    public static List<Sanpham> LaySanPhamDauGiaThanhCong(Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct dg.chitietdaugia.sanpham from Daugiathanhcong dg where dg.taikhoan=:tk";

            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);

            int numberProducts = 6;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();
        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }

        return listSP;
    }
    
    public static List<Sanpham> TimSanPhamDauGiaThanhCong(String key, Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct dg.chitietdaugia.sanpham from Daugiathanhcong dg where dg.taikhoan=:tk ";
            hql += "dg.chitietdaugia.sanpham.tenSanPham like CONCAT ('%', :tenSP ,'%')";

            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setString("tenSP", key);

            int numberProducts = 6;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();
        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }

        return listSP;
    }
    
}
